﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;
using Component.Worker.RecursiveProcess;
using System.Collections.Generic;

public partial class AdminX_Users_UsersMain : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.SetLabelLanguage();
            this.ShowMess();
            BindDataToDropdown();
            BindDataToRpUsers(1);
        }
    }
    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        lblRole.Text = MultipleLanguage.LBL_ROLE[nDefaultLanguage - 1];
        lblStatus.Text = MultipleLanguage.LBL_STATUS[nDefaultLanguage - 1];
        btnAddnew.Text = MultipleLanguage.BTN_ADDNEW[nDefaultLanguage - 1];
        Support.AddStaticData(ddlStatus, nDefaultLanguage);
    }

    private void BindDataToDropdown()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        UserRoles objRole = new UserRoles();
        DataTable dtbRole = new DataTable();
        dtbRole = objRole.Search();
        ddlRole.DataSource = dtbRole;
        ddlRole.DataValueField = "UserRoleID";
        ddlRole.DataTextField = "Description";
        ddlRole.DataBind();
        switch (nDefaultLanguage)
        {
            case 1:
                Utl.SetNonSelectToControl(ddlRole, "------ Tất cả ------", "-- No Available --");
                break;
            case 2:
                Utl.SetNonSelectToControl(ddlRole, "------ All ------", "-- No Available --");
                break;
            case 3:
                Utl.SetNonSelectToControl(ddlRole, "------ All ------", "-- No Available --");
                break;
        }

    }
    private void BindDataToRpUsers(int CurrentPageIndex)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        nDefaultLanguage = nDefaultLanguage - 1;

        int nPageCount = 0;
        int nRoleID = ConvertData.ConvertToInt(ddlRole.SelectedValue);
        int nStatus = ConvertData.ConvertToInt(ddlStatus.SelectedValue);
        Users objUser = new Users();
        DataTable dtbUser = new DataTable();
        dtbUser = objUser.Search(0, string.Empty, nStatus, nRoleID, CurrentPageIndex, Config.PAGESIZE, ref nPageCount);
        RpUser.DataSource = dtbUser;
        RpUser.DataBind();
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage] + " " + nPageCount + " " + MultipleLanguage.LBL_FOUND[nDefaultLanguage];

    }

    private void ShowMess()
    {
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        switch (nMessage)
        {
            case 1:
                MessageBoxss.Show(MessagesAlert.INSERT_SUCCESSFULL[nDefaultLang]);
                break;
            case 2:
                MessageBoxss.Show(MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLang]);
                break;
            case 3:
                MessageBoxss.Show(MessagesAlert.ITEM_NOTEXIST[nDefaultLang]);
                break;
            default:

                break;
        }
    }

    private string GetRoleNameByID(int nRoleID)
    {
        UserRoles objRoles = new UserRoles();
        objRoles.LoadById(nRoleID);
        string sReturn = ConvertData.ConvertToString(objRoles.Data.Description);
        return sReturn;
    }
    protected void RpUser_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblID = (Label)e.Item.FindControl("lblID");
            Label lblUsername = (Label)e.Item.FindControl("lblUsername");
            Label lblRole = (Label)e.Item.FindControl("lblRole");
            LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
            LinkButton lnkInactive = (LinkButton)e.Item.FindControl("lnkInactive");
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            Label lblEmail = (Label)e.Item.FindControl("lblEmail");


            lblID.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "UserID"));
            lblEmail.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Email"));
            lblUsername.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "userName"));
            int nRoleID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "RoleID"));
            lblRole.Text = GetRoleNameByID(nRoleID);
        
            
            int nStatus = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Status"));
            if (nStatus > 0)
            {
                lnkActive.Visible = true;
                lnkInactive.Visible = false;
            }
            else
            {
                lnkActive.Visible = false;
                lnkInactive.Visible = true;
            }

            lnkActive.Text = MultipleLanguage.STATUS_ACTIVE[nDefaultLanguage - 1];
            lnkInactive.Text = MultipleLanguage.STATUS_INACTIVE[nDefaultLanguage - 1];


            lnkActive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "UserID"));
            lnkActive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.INACTIVE_ALERT[nDefaultLanguage]);
            lnkInactive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "UserID"));
            lnkInactive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.ACTIVE_ALERT[nDefaultLanguage]);
            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "UserID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nDefaultLanguage]);
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "UserID"));
        }
    }
    protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataToRpUsers(1);
    }
    protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataToRpUsers(1);
    }
    protected void btnAddnew_Click(object sender, EventArgs e)
    {
        string sAddNew = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_USER_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAddNew);
    }
    protected void RpUser_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        Users objUser = new Users();
        switch (strCommand)
        {
            case "Delete":
                int nDelete = objUser.DeleteById(nID);
                //if (nDelete > 0)
                //    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[nDefaultLanguage]);
                //else
                //    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[nDefaultLanguage]);
                BindDataToRpUsers(1);
                break;

            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_USER_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;

            case "Active":
                int nActive = objUser.UpdateStatus(nID, EnumeType.INACTIVE);
                //if (nActive > 0)
                //    Alert1.ShowMessage(1, MessagesAlert.INACTIVE_SUCCESSFUL[nDefaultLanguage]);
                //else
                //    Alert1.ShowMessage(2, MessagesAlert.INACTIVE_UNSUCCESSFUL[nDefaultLanguage]);
                BindDataToRpUsers(1);
                break;

            case "Inactive":
                int nInactive = objUser.UpdateStatus(nID, EnumeType.ACTIVE);
                //if (nInactive > 0)
                //    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_SUCCESSFUL[nDefaultLanguage]);
                //else
                //    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_UNSUCCESSFUL[nDefaultLanguage]);
                BindDataToRpUsers(1);
                break;
        }
    }
}